<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\logic;

use app\model\Message;
use app\model\News;

class MessageLogic extends BaseLogic
{

    /**
     * 设置模型
     * @return string
     */
    protected function getModelName(): string
    {
        return Message::class;
    }

    public static function getMessage($params){

        $userMessage = [];
        if($params['user_id']){
            $userWhere['user_id'] = $params['user_id'];
            $userWhere['status'] = 1;
            $userMessage = Message::getMessage($userWhere);
            $userMessage = $userMessage->toArray();
        }

        $where['user_id'] = 0;
        $where['status'] = 1;
        $message = Message::getMessage($where);

        // Message::collapse($message);
        $message = $message->toArray();
        $res = array_merge($message, $userMessage);

        foreach ($res as $key => $value) {
            $res[$key]['type'] = 'message';
        }

        $newsWhere['status'] = 1;
        $news = News::where($newsWhere)->limit(20)->order('id','desc')->select();
        $news = $news->toArray();
        foreach ($news as $key => $value) {
            $news[$key]['type'] = 'news';
        }

        $res = array_merge($res, $news);
        
        // $result = array_map("unserialize", array_unique(array_map("serialize", $res)));
        $datetime = array();
        foreach ($res as $item) {
            $datetime[] = $item['update_time'];
        }
        array_multisort($datetime, SORT_DESC, $res);
        // rsort($res);
        return $res;


    }
    /**
     * @Description: 获取消息通知详情
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-15 15:15:36
     * @version: 1.0.0
     * @return [type]
     */
    public static function getMessageDetail($params){

        $where['id'] = $params['id'];
        $res = Message::where($where)->find();

        return $res;
    }


}
